package ajaxjs.data;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import ajaxjs.data.entry.type.Base;
import ajaxjs.data.entry.Entry;
import ajaxjs.data.service.Service;
public class Field{
	/**
	 * 文件名首字大写
	 * @param entity
	 * @return
	 */
	public String FirstBig(String entity){
		return entity.replaceFirst(entity.substring(0, 1), entity.substring(0, 1).toUpperCase()); 
	}
	
	/**
	 *  此处是用于 UI 字段显示的名称
	 * @param serviceInstance
	 * @return
	 */
	public static Map<String, String> getFields2Hash(Service serviceObj){
		Entry entry = serviceObj.getPojo();
		
		Map<String, String> hash = new HashMap<String, String>();
	    
	    StringBuilder buff = new StringBuilder();
	    buff.append("{");

	    for (java.lang.reflect.Field f : entry.getClass().getFields()) {
	    	String field_id, field_Label;
	    	try {
	    		field_id = f.getName();
    			Base fieldObj;
    			try{
    				fieldObj = (Base)f.get(entry);
    			}catch(ClassCastException e){ // 不是 entry 类型的，跳过
    				continue;
    			}
    			field_Label = fieldObj.label;
    			
//    			System.out.println(field_id+":"+field_Label);
    			
    			hash.put(field_id, field_Label);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	    }
	    
	    return hash;
	}
	public static String getCatalogName(Service serviceObj, Map<String, String> labels, String id){
		if(id.equals("catalog")){
			String catalogName = serviceObj.getId() + "catalog";
			return catalogName;
		}else{
			return id;
		}
	}
	public static Map<String, String> initInfEdit(Service serviceObj,HttpServletRequest request){
		Map<String, String> hash = getFields2Hash(serviceObj);
		Map<String, String> metaInfo = new HashMap<String, String>(); // 元数据
		metaInfo.put("ns_id", serviceObj.getId()); // 顺便设置命名空间
		metaInfo.put("ns_name", serviceObj.getName());
		request.setAttribute("labels", hash);
		request.setAttribute("metaInfo", metaInfo);
		
		return hash;
	}
	
	@SuppressWarnings("unchecked")
	public static void getInfoByValueObj(Entity valueObj, String methodName, HttpServletRequest request){
		if(valueObj == null)throw new NullPointerException("没有 entityInstance 参数！");
		
		java.lang.reflect.Method fn = null;
		
		try {
			fn = valueObj.getClass().getDeclaredMethod(methodName, HttpServletRequest.class);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		Map<String, Object> entity = null;
		
		try {
			entity = (Map<String, Object>)fn.invoke(valueObj, request);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.setAttribute("entityInstance_Map", entity);
	}
}
